package com.gan.simple_init.controller.sys;

import com.gan.simple_init.common.constant.UserConstant;
import com.gan.simple_init.common.base.ResponseResult;
import com.gan.simple_init.common.base.RestResultGenerator;
import com.gan.simple_init.service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author gbw
 * @date 2023/8/16
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Resource
    SysRoleService roleService;

    /**
     * 获取角色列表
     *
     * @return
     */
    @ApiOperation("获取角色名列表")
    @GetMapping("/list")
    @PreAuthorize(UserConstant.PRE_ROLE_NAME_ADMIN)
    public ResponseResult<?> getRoleList() {
        return RestResultGenerator.success(roleService.selectRoleNameList());
    }

    /**
     * 根据 id 修改用户角色
     *
     * @return
     */
    @ApiOperation("根据 id 修改/新增用户角色")
    @GetMapping("/updateById")
    @PreAuthorize(UserConstant.PRE_ROLE_NAME_ADMIN)
    public ResponseResult<?> updateRoleById(@ApiParam(required = true) Long userId, @ApiParam(required = true) Long roleId) {
        if (roleService.updateRoleById(userId, roleId)) {
            return RestResultGenerator.success("修改成功!");
        } else {
            return RestResultGenerator.error("修改失败!");
        }
    }
}
